package tank.po;

import tank.Enum.Dir;
import tank.Enum.Group;
import tank.util.Audio;
import tank.util.PropertyMgr;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public class TankFrame extends Frame {

    Tank tank = new Tank(200, 200, Dir.DOWN, this, Group.GOOD);
    List<Bullet> bullets = new ArrayList<>();
    //Bullet bullet = new Bullet(300,300,Dir.DOWN);
    List<Tank> tanks = new ArrayList<>();
    List<Explode> explodes = new ArrayList<>();
    Explode e = new Explode(100,100,this);
    static final int GAME_WIDTH = Integer.parseInt((String) PropertyMgr.get("gameWidth")), GAME_HEIGHT = Integer.parseInt((String)PropertyMgr.get("gameHeight"));


    public TankFrame() {
        setSize(GAME_WIDTH, GAME_HEIGHT);
        setResizable(false);
        setTitle("tank war");
        setVisible(true);

        this.addKeyListener(new MyKeyListener());
        //添加关闭的监听事件  方法体内为点击事件的执行方法
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }


    //双缓冲解决闪烁问题
    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(GAME_WIDTH, GAME_WIDTH);
        }
        Graphics goffScreen = offScreenImage.getGraphics();
        Color c = goffScreen.getColor();
        goffScreen.setColor(Color.black);
        goffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
        goffScreen.setColor(c);
        paint(goffScreen);
        g.drawImage(offScreenImage, 0, 0, null);

    }

    //使用画笔定义一个方框
    @Override
    public void paint(Graphics g) {
        Color c = g.getColor();
        g.setColor(Color.WHITE);
        g.drawString("子弹的数量：" + bullets.size(), 10, 40);
        g.drawString("坦克的数量：" + tanks.size(), 10, 60);
        g.drawString("爆炸的数量" +  explodes.size(), 10,80);
        g.setColor(c);

        tank.paint(g);
        for (int i = 0; i < tanks.size(); i++) {
            tanks.get(i).paint(g);
        }
        for (int i = 0; i < explodes.size(); i++) {
            explodes.get(i).paint(g);
        }
        //普通方法去或画子弹
        for (int i = 0; i < bullets.size(); i++) {
            bullets.get(i).paint(g);
        }
//迭代器有同步状态 不允许外部对其进行增删改 自身可以修改  容器的迭代器知识
//        for (Bullet bullet:bullets){
//            bullet.paint(g);
//        }
//        //迭代器的方法去删除
//        for(Iterator<Bullet> it = bullets.iterator();it.hasNext()){
//            Bullet bullet = it.next();
//            if(!bullet.live) it.remove();
//        }
        //遍历碰撞
        for (int i = 0; i < bullets.size(); i++) {
            for (int j = 0; j < tanks.size(); j++) {
                bullets.get(i).collideWith(tanks.get(j));
            }
        }
    }

    //这个类只有tankFrame使用 定义键盘监听事件
    class MyKeyListener extends KeyAdapter {
        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;

        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                default:
                    break;
            }
            //设定主战坦克的方向
            setMainTankDir();
            //默认调用paint方法 进行重画
            //repaint();
            //new出来一个线程调用声音
            new Thread(()->new Audio("audio/tank_move.wav").play()).start();
        }

        @Override
        public void keyReleased(KeyEvent e) {
            //y+=200;
            //默认调用paint方法 进行重画
            //repaint();
            int key1 = e.getKeyCode();
            switch (key1) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                //将子弹和tank绑定起来
                case KeyEvent.VK_CONTROL:
                    tank.fire();
                default:
                    break;
            }
            //设定主战坦克的方向
            setMainTankDir();
        }

        //根据按键的布尔值来改变按键的方向
        private void setMainTankDir() {
            if (!bL && !bU && !bR && !bD) tank.setMoving(false);
            else {
                tank.setMoving(true);
                if (bL) tank.setDir(Dir.LEFT);
                if (bU) tank.setDir(Dir.UP);
                if (bR) tank.setDir(Dir.RIGHT);
                if (bD) tank.setDir(Dir.DOWN);
            }
        }
    }

}
